#This script executes event-study regressions to estimate cohort-specific 
#associations between first-generation prenatal exposure to nonattainment 
#and first-generation assortative matching outcomes.

rm(list=ls())
gc()
library(reldist)
library(ineq)
library(stargazer)
library(data.table)
library(dplyr)
library(dtplyr)
library(ggplot2)
library(stringdist)
library(lfe)
library(haven)
library(readr)
library(broom)
library(tidylog)
library(rdrobust)

# Set the working directory
setwd("/projects/opp_env/caa1970/")

# Source external R scripts for custom functions
source("Code/Child_Outcomes/child_outcomes_functions.R")
source("Code/rounding.r")

# Load the first-generation assortative matching analysis dataset.
load("Data/first_gen_analysis_data_jepmicro_assortative.rda")

# Event Studies

eventB4a <- felm(partner_treated ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,
               data=parent_piks_assortative%>% filter(year%in%1969:1975, AGE >33 & AGE < 44, married == 1,!is.na(all_fullterm),!is.na(partner_treated),!is.na(both_employed), !is.na(partner_higher_earnings),  real_wages<wage_cap, !is.na(parent_age_min)))
summary(eventB4a)

#Save the underlying point
figB4a <- eventB4a %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB4a, file="JPE_Micro/Output/B4_Figures/FigB4a.csv", row.names=F)



eventB4b <- felm(both_employed ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,
               data=parent_piks_assortative%>% filter(year%in%1969:1975, AGE >33 & AGE < 44, married == 1,!is.na(all_fullterm),!is.na(partner_treated),!is.na(both_employed), !is.na(partner_higher_earnings),  real_wages<wage_cap, !is.na(parent_age_min)))
summary(eventB4b)

#Save the underlying point
figB4b <- eventB4b %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB4b, file="JPE_Micro/Output/B4_Figures/FigB4bcsv", row.names=F)


eventB4c <- felm(both_college ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,
               data=parent_piks_assortative%>% filter(year%in%1969:1975, AGE >33 & AGE < 44, married == 1,!is.na(all_fullterm),!is.na(partner_treated),!is.na(both_employed), !is.na(partner_higher_earnings),  real_wages<wage_cap, !is.na(parent_age_min)))
summary(eventB4c)

#Save the underlying point
figB4c <- eventB4c %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(event_1, file="JPE_Micro/Output/B4_Figures/FigB4c.csv", row.names=F)


eventB4d <- felm(partner_higher_earnings ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,
               data=parent_piks_assortative%>% filter(year%in%1969:1975, AGE >33 & AGE < 44, married == 1,!is.na(all_fullterm),!is.na(partner_treated),!is.na(both_employed), !is.na(partner_higher_earnings),  real_wages<wage_cap, !is.na(parent_age_min)))
summary(eventB4d)

#Save the underlying point
figB4d <- eventB4d %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB4d, file="JPE_Micro/Output/B4_Figures/FigB4d.csv", row.names=F)


